package com.cy4;

import java.util.Arrays;

/**
 * 8月1日作业(Arrays)
 *
 * @author xxx
 * @date 2022/08/01 16:33
 **/
public class HomeWork {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        // 改变原有数组元素的值
        // transform(arr);
        // System.out.println(Arrays.toString(arr));
        // 求数组的平均值
        // System.out.println(avg(arr));
        // 奇数位于偶数之前
        // System.out.println("调整前：" + Arrays.toString(arr));
        // changeArrays(arr);
        // System.out.println("调整后：" + Arrays.toString(arr));
        //
        // 给定一个有序整型数组, 实现二分查找
        // System.out.println(myBinarySearch(arr, 5));
        // 两数之和
        // int[] arr1 = {2, 7, 11, 15};
        // System.out.println(Arrays.toString(twoNumbersSum(arr1, 9)));
        // 只出现一次的数字
        // int[] arr2 = {4, 1, 2, 1, 2};
        // System.out.println("单身狗：" + numbersThatAppearOnlyOnce(arr2));
        // 多数元素
        // int[] arr3 = {2, 2, 1, 1, 1, 2, 2};
        // System.out.println("多数元素："+mostElements(arr3));
        // 存在连续三个奇数的数组
        int[] arr4 = {1, 2, 34, 3, 4, 5, 7, 23, 12};
        // System.out.println(thereAreThreeConsecutiveArraysOfOddNumbers(arr4));
        int[] arr5 = null;
        thereAreThreeConsecutiveArraysOfOddNumbers(arr5);

    }

    private static boolean thereAreThreeConsecutiveArraysOfOddNumbers(int[] arr4) {
        assert arr4 != null : "Connection is null";
        for (int i = 0; i < arr4.length; i++) {
            if (((i + 2 <= arr4.length - 1) && (arr4[i] % 2 != 0) && (arr4[i + 1]) % 2 != 0)
                && (arr4[i + 2] % 2 != 0)) {
                return true;
            }
        }
        return false;
    }

    private static int mostElements(int[] arr3) {
        // 摩尔投票
        int ret = arr3[0];
        int cnt = 1;
        for (int i = 1; i < arr3.length; i++) {
            if (arr3[i] == ret) {
                cnt++;
            } else {
                if (--cnt == 0) {
                    ret = arr3[i];
                    cnt = 1;
                }
            }
        }
        return ret;
    }

    private static int numbersThatAppearOnlyOnce(int[] arr2) {
        flag:
        for (int i = 0; i < arr2.length; i++) {
            for (int i1 = 0; i1 < arr2.length; i1++) {
                if (arr2[i1] == arr2[i] && i1 != i) {
                    continue flag;
                }
                return arr2[i1];
            }
        }
        return -1;
    }

    private static int[] twoNumbersSum(int[] arr1, int target) {
        int[] ret = new int[2];
        for (int i = 0; i < arr1.length; i++) {
            for (int i1 = 0; i1 < arr1.length; i1++) {
                if (arr1[i1] + arr1[i] == target && i != i1) {
                    ret[0] = i;
                    ret[1] = i1;
                }
            }
        }
        return ret;
    }

    private static int myBinarySearch(int[] arr, int i) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] < i) {
                left = mid + 1;
            } else if (arr[mid] > i) {
                right = mid - 1;
            } else {
                return mid;
            }
        }
        return -1;
    }

    private static void changeArrays(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        int[] tmp = Arrays.copyOf(arr, arr.length);
        for (int i : tmp) {
            if (i % 2 == 0) {
                arr[right--] = i;
            } else {
                arr[left++] = i;
            }
        }
    }

    private static double avg(int[] arr) {
        double sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum * 1.0 / arr.length;
    }

    private static void transform(int[] arr) {
        Arrays.setAll(arr, i -> 2 * arr[i]);
    }
}
